package com.nxu.dao.impl;

import com.nxu.dao.CardDao;
import com.nxu.entity.CardBean;
import com.nxu.dao.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CardDaoImpl implements CardDao {
    @Override
    public boolean addCard(CardBean card) {
        String sql = "INSERT INTO user_cards (user_id, card_number, card_password, balance) VALUES (?, ?, ?, ?)";
        try (Connection conn = BaseDao.getConn();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, card.getUserId());
            pstmt.setString(2, card.getCardNumber());
            pstmt.setString(3, card.getCardPassword());
            pstmt.setDouble(4, card.getBalance());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean verifyCardExists(String cardNumber) {
        String sql = "SELECT COUNT(*) FROM user_cards WHERE card_number = ?";
        try (Connection conn = BaseDao.getConn();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, cardNumber);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                return rs.getInt(1) > 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean verifyCardPassword(String cardNumber, String password) {
        String sql = "SELECT COUNT(*) FROM user_cards WHERE card_number = ? AND card_password = ?";
        try (Connection conn = BaseDao.getConn();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, cardNumber);
            pstmt.setString(2, password);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                return rs.getInt(1) > 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public double getCardBalance(String cardNumber) {
        String sql = "SELECT balance FROM user_cards WHERE card_number = ?";
        try (Connection conn = BaseDao.getConn();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, cardNumber);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                return rs.getDouble("balance");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0.0;
    }

    @Override
    public boolean updateBalance(String cardNumber, double newBalance) {
        String sql = "UPDATE user_cards SET balance = ? WHERE card_number = ?";
        try (Connection conn = BaseDao.getConn();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setDouble(1, newBalance);
            pstmt.setString(2, cardNumber);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}